package com.canaan.business.service.bill.impl;

import java.util.Date;
import java.util.List;

import com.canaan.business.domain.bill.*;
import com.canaan.business.service.bill.IReceiveProductBillService;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.bill.ReceiveProductBillDetailMapper;
import com.canaan.business.service.bill.IReceiveProductBillDetailService;

/**
 * 收货单明细Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-01
 */
@Service
public class ReceiveProductBillDetailServiceImpl implements IReceiveProductBillDetailService {
    @Resource
    private ReceiveProductBillDetailMapper receiveProductBillDetailMapper;
    @Resource
    private IReceiveProductBillService receiveProductBillService;

    /**
     * 查询收货单明细
     *
     * @param id 收货单明细主键
     * @return 收货单明细
     */
    @Override
    public ReceiveProductBillDetail selectReceiveProductBillDetailById(Long id) {
        return receiveProductBillDetailMapper.selectReceiveProductBillDetailById(id);
    }

    /**
     * 查询收货单明细列表
     *
     * @param receiveProductBillDetail 收货单明细
     * @return 收货单明细
     */
    @Override
    public List<ReceiveProductBillDetail> selectReceiveProductBillDetailList(ReceiveProductBillDetail receiveProductBillDetail) {
        return receiveProductBillDetailMapper.selectReceiveProductBillDetailList(receiveProductBillDetail);
    }

    /**
     * 新增收货单明细
     *
     * @param receiveProductBillDetail 收货单明细
     * @return 结果
     */
    @Override
    public int insertReceiveProductBillDetail(ReceiveProductBillDetail receiveProductBillDetail) {
        if(StringUtils.isBlank(receiveProductBillDetail.getNo())){
            receiveProductBillDetail.setNo(TableNoUtil.getNo(ReceiveProductBillDetail.class));
        }
        if(StringUtils.isEmpty(receiveProductBillDetail.getCreateBy())){
            receiveProductBillDetail.setCreateBy(SecurityUtils.getUsername());
        }
        receiveProductBillDetail.setCreateTime(DateUtils.getNowDate());
        int i = receiveProductBillDetailMapper.insertReceiveProductBillDetail(receiveProductBillDetail);
//        refreshTotalQty(receiveProductBillDetail.getBillNo());
        return i;
    }

    private void refreshTotalQty(String billNo){
        ReceiveProductBillDetail detail = new ReceiveProductBillDetail();
        detail.setBillNo(billNo);
        List<ReceiveProductBillDetail> details = this.selectReceiveProductBillDetailList(detail);
        ReceiveProductBill bill = receiveProductBillService.selectReceiveProductBillByNo(billNo);
        bill.setTotalQty(details.stream().filter(e->e.getQty()!=null).filter(e->e.getQty()!=null).mapToLong(e -> e.getQty()).sum());
//        // 如果状态是待进行，需要改成进行中
//        if (OrderStatusEnum.NEW.getCode().equals(bill.getStatus())){
//            bill.setStatus(OrderStatusEnum.IN_HAND.getCode());
//        }
        receiveProductBillService.updateReceiveProductBill(bill);
    }

    /**
     * 批量新增收货单明细
     *
     * @param receiveProductBillDetailList 收货单明细列表
     * @return 结果
     */
    @Override
    public int batchInsertReceiveProductBillDetail(List<ReceiveProductBillDetail> receiveProductBillDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (ReceiveProductBillDetail receiveProductBillDetail : receiveProductBillDetailList){
            if(StringUtils.isBlank(receiveProductBillDetail.getNo())){
                receiveProductBillDetail.setNo(TableNoUtil.getNo(ReceiveProductBillDetail.class));
            }
            receiveProductBillDetail.setCreateBy(username);
            receiveProductBillDetail.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<receiveProductBillDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > receiveProductBillDetailList.size()) {
                endIndex = receiveProductBillDetailList.size();
            }
            insertNum = insertNum + receiveProductBillDetailMapper.batchInsertReceiveProductBillDetail(receiveProductBillDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改收货单明细
     *
     * @param receiveProductBillDetail 收货单明细
     * @return 结果
     */
    @Override
    public int updateReceiveProductBillDetail(ReceiveProductBillDetail receiveProductBillDetail) {
        receiveProductBillDetail.setUpdateBy(SecurityUtils.getUsername());
        receiveProductBillDetail.setUpdateTime(DateUtils.getNowDate());
        int i = receiveProductBillDetailMapper.updateReceiveProductBillDetail(receiveProductBillDetail);
        refreshTotalQty(receiveProductBillDetail.getBillNo());
        return i;
    }

    /**
     * 批量修改收货单明细
     *
     * @param receiveProductBillDetailList 收货单明细列表
     * @return 结果
     */
    @Override
    public int batchUpdateReceiveProductBillDetail(List<ReceiveProductBillDetail> receiveProductBillDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (ReceiveProductBillDetail receiveProductBillDetail : receiveProductBillDetailList){
            receiveProductBillDetail.setUpdateBy(username);
            receiveProductBillDetail.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<receiveProductBillDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > receiveProductBillDetailList.size()) {
                endIndex = receiveProductBillDetailList.size();
            }
            updateNum = updateNum + receiveProductBillDetailMapper.batchUpdateReceiveProductBillDetail(receiveProductBillDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除收货单明细
     *
     * @param ids 需要删除的收货单明细主键集合
     * @return 结果
     */
    @Override
    public int deleteReceiveProductBillDetailByIds(Long[] ids) {
        int i = receiveProductBillDetailMapper.deleteReceiveProductBillDetailByIds(ids);
        ReceiveProductBillDetail receiveProductBillDetail = this.selectReceiveProductBillDetailById(ids[0]);
        refreshTotalQty(receiveProductBillDetail.getBillNo());
        return i;
    }

    /**
     * 删除收货单明细信息
     *
     * @param id 收货单明细主键
     * @return 结果
     */
    @Override
    public int deleteReceiveProductBillDetailById(Long id) {
        return receiveProductBillDetailMapper.deleteReceiveProductBillDetailById(id);
    }

    /**
     * 查询收货单明细
     *
     * @param no 编号
     * @return 收货单明细
     */
    @Override
    public ReceiveProductBillDetail selectReceiveProductBillDetailByNo(String no) {
        return receiveProductBillDetailMapper.selectReceiveProductBillDetailByNo(no);
    }

    /**
     * 批量删除收货单明细
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteReceiveProductBillDetailByNos(String[] nos) {
        return receiveProductBillDetailMapper.deleteReceiveProductBillDetailByNos(nos);
    }

    /**
     * 删除收货单明细信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteReceiveProductBillDetailByNo(String no) {
        return receiveProductBillDetailMapper.deleteReceiveProductBillDetailByNo(no);
    }
}
